package com.beone.admin.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.base.SuperController;
import com.base.common.util.DateUtil;
import com.beone.admin.annotation.Log;
import com.beone.admin.entity.BaseUser;
import com.beone.admin.service.BaseUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @title 控制台 控制器
 * @Author 覃球球
 * @Version 1.0 on 2018/12/20.
 * @Copyright 贝旺科技
 */
@Controller
@RequestMapping("/system/console")
public class ConsoleController extends SuperController {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(ConsoleController.class);
    @Autowired
    private BaseUserService userService;

    /**
     * 控制台 index -> 入口
     *
     * @return
     */
    @GetMapping("")
    public String index(ModelMap model, HttpServletRequest request) {
        //获取当前日期 格式为yyyy-MM-dd
        model.addAttribute("currentDate", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd"));
        BaseUser user = ControllerUtils.getAdminUser(request);
        request.setAttribute("userPhone",user.getUserMobile());
        return "my/console";
    }


    /**
     * 我的账户 -> 入口
     *
     * @return
     * @Author 肖颖欢
     */
    @GetMapping("/my")
    public String my(ModelMap model, HttpServletRequest request) {
        BaseUser user = ControllerUtils.getAdminUser(request);
        model.addAttribute("user", user);
        model.addAttribute("lastVisit", DateUtil.timeStamp2Date(user.getLastVisit(), "yyyy-MM-dd HH:mm:ss"));
        return "my/my";
    }


    /**
     * 更新用户手机号码
     *
     * @param userMobile 手机号码
     * @return 添加结果
     */
    @Log(desc = "用户", type = Log.LOG_TYPE.SAVE)
    @PostMapping(value = "/saveUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @ApiOperation(value = "更新用户手机号码", notes = "更新用户手机号码")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true
            , paramType = "form", dataType = "object")
    public Object saveUser(HttpServletRequest request, String userMobile, String email) {
        JSONObject jsonObject = new JSONObject();
        BaseUser user = ControllerUtils.getAdminUser(request);
        BaseUser entity = new BaseUser();
        entity.setUserMobile(userMobile);
        entity.setEmail(email);
        entity.setUserId(user.getUserId());
        boolean result = userService.updateUserMobileByUserId(entity);
        if (result) {
            user.setUserMobile(userMobile);//数据库里面修改数据成功后同步session里面的数据
            user.setEmail(email);
            jsonObject.put("code", "success");
            jsonObject.put("desc", "成功");
        } else {
            jsonObject.put("code", "fail");
            jsonObject.put("desc", "失败");
        }
        return jsonObject;
    }
}
